window.YiHang = window.YiHang ? window.YiHang : {};

window.YiHang.deepMerge = function (obj1, obj2) {
	var key;
	for (key in obj2) {
		// 如果target(也就是obj1[key])存在，且是对象的话再去调用deepMerge，否则就是obj1[key]里面没这个对象，需要与obj2[key]合并
		obj1[key] = obj1[key] && obj1[key].toString() === "[object Object]" ?
			YiHang.deepMerge(obj1[key], obj2[key]) : obj1[key] = obj2[key];
	}
	return obj1;
}

window.YiHang.alertOptions = {
	title: '提示',
	content: '',
	backgroundColor: '#ffffff',
	themeColor: '#409eff',
	maskClose: true,
	buttonContent: '确定',
	buttonColor: '#ffffff',
}

window.YiHang.alertClass = class {

	box = null;

	show(options = {}) {
		// 合并配置
		this.options = Object.assign({}, YiHang.alertOptions, options);

		// 创建弹窗元素
		this.box = document.createElement('div');
		this.box.className = 'YiHangAlert';
		this.box.style.backgroundColor = this.options.backgroundColor;

		// 标题
		const title = document.createElement('div');
		title.className = 'YiHangAlertTitle';
		title.innerHTML = this.options.title;
		this.box.appendChild(title);

		// 内容
		const content = document.createElement('div');
		content.className = 'YiHangAlertContent';
		content.innerHTML = this.options.content;
		this.box.appendChild(content);

		// 按钮组
		const buttonsContainer = document.createElement('div');
		buttonsContainer.className = 'YiHangAlertButtons';

		this.options.buttons = this.options.buttons ? this.options.buttons : [];

		// 按钮属性
		this.options.buttons.forEach((button, index) => {
			const buttonElement = document.createElement('button');
			// 合并属性
			const buttonAttributes = Object.assign({}, {}, button.attributes);
			// 设置按钮属性
			for (const key in buttonAttributes) {
				buttonElement.setAttribute(key, buttonAttributes[key]);
			}
			// 添加类名
			console.log(button);
			buttonElement.classList.add('YiHangAlertButton');
			button.classList = button.classList ? button.classList : [];
			buttonElement.classList.add(...button.classList);
			// 按钮文字
			buttonElement.innerHTML = button.text ? button.text : YiHang.alertOptions.buttonContent;
			// 按钮背景颜色
			buttonElement.style.backgroundColor = button.backgroundColor || this.options.themeColor;
			// 按钮文字颜色
			buttonElement.style.color = button.color ? button.color : YiHang.alertOptions.buttonColor;
			// 按钮回调函数
			buttonElement.onclick = () => {
				if (button.callback) {
					button.callback();
				}
				this.close();
			};
			buttonsContainer.appendChild(buttonElement);
		});
		this.box.appendChild(buttonsContainer);
		setTimeout(() => {
			this.box.classList.add('show');
		}, 150);

		// 添加弹窗到页面
		document.body.appendChild(this.box);

		// 创建遮罩层
		this.mask = document.createElement('div');
		this.mask.className = 'YiHangAlertMask';
		document.body.appendChild(this.mask);
		setTimeout(() => {
			this.mask.classList.add('show')
		}, 150);
		if (this.options.maskClose) this.mask.onclick = () => {
			this.close();
		}
		return this;
	}

	close() {
		this.box.classList.remove('show');
		this.box.classList.add('hide');

		this.mask.classList.remove('show');
		this.mask.classList.add('hide');

		setTimeout(() => {
			this.box.remove();
			this.mask.remove();
		}, 150);
	}
};
window.YiHang.alert = (options = {}, global = false) => {
	if (global) {
		YiHang.alertOptions = Object.assign({}, YiHang.alertOptions, options);
	} else {
		return (new YiHang.alertClass).show(options);
	}
}